import { Node, Vec2, Vec3 } from "cc";
import { BaseEntityMgr, EEntityState, IVector2, NodeHelper, P2Body, TVectorSet, _logic } from "../../main/script/Main";
import { Scene } from "./Scene";
import { CResoucesLoadPriority, CSceneData, EEntityType, ILogicData, ILogicNuoData } from "../../main/script/module/define/LogicDefine";
import { Entity } from "./entity/Entity";
import { CubeEntity } from "./entity/CubeEntity";
import { MoveAnimEntity } from "./entity/MoveAnimEntity";
import { ISelectionEntityData, SelectionEntity } from "./entity/SelectionEntity";
import { CubeAnimEntity } from "./entity/CubeAnimEntity";
import { DragEntity } from "./entity/DragEntity";
import { TipEntity } from "./entity/TipEntity";
import { CubeNuoEntity } from "./entity/CubeNuoEntity";



const v2T = new Vec2()
const v3T = new Vec3()

export class EntityMgr extends BaseEntityMgr<Scene, Entity> {

    public get drag(): DragEntity {
        let entites = this.entitesType.get(EEntityType.Drag)
        if (!entites)
            return null!
        return entites.shiftValue()
    }

    protected getQueueResoucesPriority(): number[] { return CResoucesLoadPriority }

    public createCube(data: ILogicData, animComplete?: () => void, addChildIndex = -1) {
        let entity = this.create<CubeEntity>(CSceneData.cubePrefabUrl)
        entity.animComplete = animComplete
        entity.data = data
        entity.entityStateMackine.change(EEntityState.Load)
        if (addChildIndex == -1)
            this.scene.cACube.addChild(entity.node)
        else
            this.scene.cACube.insertChild(entity.node, addChildIndex)
        entity.entityStateMackine.change(EEntityState.Reset)
        entity.entityStateMackine.change(EEntityState.Run)

        return entity
    }

    public createCubeNuo(data: ILogicNuoData, animComplete?: () => void, addChildIndex = -1) {
        let entity = this.create<CubeNuoEntity>(CSceneData.cubeNuoPrefabUrl)
        entity.animComplete = animComplete
        entity.data = data
        entity.entityStateMackine.change(EEntityState.Load)
        if (addChildIndex == -1)
            this.scene.cACube.addChild(entity.node)
        else
            this.scene.cACube.insertChild(entity.node, addChildIndex)
        entity.entityStateMackine.change(EEntityState.Reset)
        entity.entityStateMackine.change(EEntityState.Run)

        return entity
    }

    public createDrag() {
        let entity = this.create<DragEntity>(CSceneData.dragPrefabUrl)
        entity.entityStateMackine.change(EEntityState.Load)
        this.scene.cADragParent.addChild(entity.node)
        entity.entityStateMackine.change(EEntityState.Reset)
        entity.entityStateMackine.change(EEntityState.Run)

        return entity
    }

    public createMoveAnim(
        posY: number[],
        endIndex: number,
        width: number,
        animComplete: () => void
    ) {
        let entity = this.create<MoveAnimEntity>(CSceneData.moveAnimPrefabUrl)
        entity.animComplete = animComplete
        entity.posY = posY
        entity.width = width
        entity.endIndex = endIndex
        entity.entityStateMackine.change(EEntityState.Load)
        this.scene.cAEffect.addChild(entity.node)
        entity.entityStateMackine.change(EEntityState.Reset)
        entity.entityStateMackine.change(EEntityState.Run)

        return entity
    }


    public createSelection(data: ISelectionEntityData) {
        let entity = this.create<SelectionEntity>(CSceneData.selectionPrefabUrl)
        entity.data = data
        entity.entityStateMackine.change(EEntityState.Load)
        this.scene.cAEffect.addChild(entity.node)
        entity.entityStateMackine.change(EEntityState.Reset)
        entity.entityStateMackine.change(EEntityState.Run)

        return entity
    }

    public createSelectionNuo(data: ILogicNuoData) {
        return this.createSelection((data as any).entity)
    }

    public createTip(data: ISelectionEntityData) {
        let entity = this.create<TipEntity>(CSceneData.tipPrefabUrl)
        entity.data = data
        entity.entityStateMackine.change(EEntityState.Load)
        this.scene.cAEffect.addChild(entity.node)
        entity.entityStateMackine.change(EEntityState.Reset)
        entity.entityStateMackine.change(EEntityState.Run)

        return entity
    }

    public createTipNuo(data: ILogicNuoData) {
        return this.createTip((data as any).entity)
    }


    public createCubeAnim() {
        let entity = this.create<CubeAnimEntity>(CSceneData.cubeAnimPrefabUrl)
        entity.entityStateMackine.change(EEntityState.Load)
        this.scene.cAEffect.addChild(entity.node)
        entity.entityStateMackine.change(EEntityState.Reset)
        entity.entityStateMackine.change(EEntityState.Run)

        return entity
    }

}